﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;
using Wicresoft.RDW.Model;
using Quartz.Impl;
using System.Configuration;
using System.Diagnostics;
using Common.Logging;

namespace Wicresoft.RDW.Service
{
    public class RegisterFeedBackRemind : IJob
    {
        private static ISchedulerFactory schedFact = new StdSchedulerFactory();
        private static ILog log = LogManager.GetLogger(typeof(RegisterFeedBackRemind));
        
        public void Execute(JobExecutionContext context)
        {
            log.Info(DateTime.Now+"  registe request which should be send reminder mail for serveral days");
            var unFeedbackRequestList = GetUnFeedbackRequest();
            AddFeedbackReminderTriggerForRequest(unFeedbackRequestList);
            log.Info("registe feedback job ok");
        }
        /// <summary>
        /// 获取没有添加反馈的request
        /// </summary>
        /// <returns></returns>
        private List<Request> GetUnFeedbackRequest()
        {
            log.Info("  GetUnFeedbackRequest()");
            var requestList = RequestPresent.GetList(x => x.Status == (int)RequestStatus.Completed&&x.RequestType==(int)RequestType.Visit);
            var unFeedbackRequestList = new List<Request>();
            foreach (var request in requestList)
            {
                var feedBack = FeedbackPresent.GetByCondition(x => x.Status == (int)FeedbackStatus.Active && x.RequestId == request.RequestId);
                if (feedBack != null)
                {
                    unFeedbackRequestList.Add(request);
                }
            }
            log.Info("unFeedbackRequestList.Count:" + unFeedbackRequestList.Count);
            return unFeedbackRequestList;
        }
        /// <summary>
        /// 为没有添加反馈的request添加连续发邮件消息注册
        /// </summary>
        /// <param name="requestList"></param>
        private void AddFeedbackReminderTriggerForRequest(List<Request> requestList)
        {
            log.Info(" AddFeedbackReminderTriggerForRequest(List<Request> requestList)");
            var sendFeedBackReminderTime = ConfigurationManager.AppSettings["SendFeedBackReminderTime"];
           
            foreach (var request in requestList)
            {
                IScheduler sched = schedFact.GetScheduler();
                var requestId = request.RequestId;
                var sysSetting = SysSettingPresent.GetByCondition(x => x.Key == "FeedbackEmailRemindDay");
                if (sysSetting == null)
                {
                    sysSetting = new SysSetting() { Value="5"};
                }
                DateTime time = DateTime.UtcNow;
                JobDetail feedBackReminderJob = new JobDetail("FeedBackReminder" + requestId, "group", typeof(ExecFeedbackReminder));
                if (sendFeedBackReminderTime == null)
                {
                    
                    time = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, 9, 0, 0);
                    if (DateTime.UtcNow > time)
                    {
                        time = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day+1, 9, 0, 0);
                    }
                    
                }
                else
                {
                    DateTime setting = Convert.ToDateTime(sendFeedBackReminderTime);
                    time = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, setting.Hour, setting.Minute, 0);
                    if (DateTime.UtcNow > time)
                    {
                        time = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day + 1, setting.Hour, setting.Minute, 0);
                    }
                }
                
                TimeSpan span = new TimeSpan(24, 0, 0);
                var repeatCount = int.Parse(sysSetting.Value);
                SimpleTrigger trigger = new SimpleTrigger("FeedBackReminder" + requestId, "group", time,null, repeatCount, span);
                feedBackReminderJob.JobDataMap["RepeatCount"] = repeatCount;
                feedBackReminderJob.JobDataMap[feedBackReminderJob.Name] = requestId;
                if (!FeedBackJobList.Jobs.ContainsKey(feedBackReminderJob.Name))
                {
                    sched.ScheduleJob(feedBackReminderJob, trigger);
                    FeedBackJobList.Jobs.Add(feedBackReminderJob.Name, feedBackReminderJob.Name);
                    log.Info("registe job name :" + feedBackReminderJob.Name);
                }
                if (!sched.IsStarted)
                {
                    sched.Start();
                }
            }
        }
    }
}
